from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
from mall import settings


def generic_access_tocken(openid):
    # 生成序列化器
    serializer = Serializer(settings.SECRET_KEY, 3600)
    # 对敏感的数据进行校验
    token = serializer.dumps({'openid': openid})  # 加密
    # 返回数据
    return token.decode()


def check_open_id(token):
    # 生成序列化器
    serializer = Serializer(settings.SECRET_KEY, 3600)
    # 校验
    try:
        result = serializer.loads(token)  # 解密
        # result就是当时加密的数据
    except BadSignature:
        return None
    else:
        return result.get('openid')
